package com.hiwan.unions.view.popupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.membership.MembershipTransferActivity;
import com.hiwan.unions.activity.membership.MembershipUpdateActivity;

/**
 * Created by llg on 2017/7/9.
 */
public class SetCheckPop extends PopupWindow {
    private Activity activity;
    private View conentView;

    public SetCheckPop(final Activity context, final Bundle bundle) {
        this.activity = context;
        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.popwindow_check, null);
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 刷新状态
        this.update();
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        conentView.setFocusable(true);
        conentView.setFocusableInTouchMode(true);
        conentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    dismiss();
                return false;
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1f;
                activity.getWindow().setAttributes(params);
            }
        });

        findView(conentView, R.id.TextEndAdd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findView(conentView, R.id.custom1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MembershipUpdateActivity.startPreview(activity, bundle);
                dismiss();
            }
        });
        findView(conentView, R.id.custom2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MembershipTransferActivity.startPreview(activity);
                dismiss();
            }
        });
    }


    public <T extends View> T findView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }


    public void showPopupWindow(View view) {
        if (!this.isShowing()) {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 0.6f; // 0.0-1.0
            activity.getWindow().setAttributes(lp);
            // 以下拉方式显示popupwindow
            this.showAtLocation(view,
                    Gravity.BOTTOM, 0, 0);
        }
    }
}

